<template>
  <div class="app-container">
    <div class="search-area" style="margin-bottom: 50px">
      <el-input v-model="listQuery.query" placeholder="请输入内容"  style="width: 300px;float: right;" clearable=""><el-button slot="append" icon="el-icon-search" @click="search"></el-button></el-input>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column  align="center" label="职位名称" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="职位类别" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.postTypeName }}</span>
        </template>
      </el-table-column>

      <el-table-column   label="工作地点" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.addressDetail | addressFilter(cityAll) }}</span>
          <!-- <span>{{ scope.row.areaCodeName || '-'}}</span> -->
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="薪资待遇" width="110" show-overflow-tooltip>
        <template slot-scope="scope">
         <span>{{scope.row.salaryMin}}</span>-<span>{{scope.row.salaryMax}}</span>
        </template>
      </el-table-column>

      <el-table-column  label="发布时间" width="160" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column  label="状态" width="200" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.recruitmentState | recruitState}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="350" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-row :gutter="3">
            <el-col :span="6"> <el-button  type="warning" size="small" @click="recruitStateSwitch(scope.row)">{{scope.row.recruitmentState >= 0 ? '下架' : '上架'}}</el-button></el-col>
            <el-col :span="6"> <el-button type="primary" size="mini" @click="edit(scope.row.id)">编辑</el-button></el-col>
            <el-col :span="6"> <el-button  type="danger" size="small" @click="delPosition(scope.row)">删除</el-button></el-col>
            <el-col :span="6"> <el-button  type="primary" size="small" @click="screenClick">筛选人才</el-button></el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { fetchList } from '@/api/web/post'

  export default {
    name:"Index",
    data() {
      return {
        list: [],
        listLoading: false,
        listQuery: {
          pageNo: 1,
          limit: 10,
          query: ""
        },
        adressData: []
      }
    },
    computed: {
      ...mapGetters([
        'cityAll'
      ])
    },
    created() {
      this.init()
    },
    methods: {
      recruitStateSwitch (el) { // 下架 or 上架
        this.$confirm(`确定${el.recruitmentState >= 0 ? '下架' : '上架'}该职位, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {
            postId: el.id,
            useable: el.recruitmentState >= 0 ? false : true
          }
          this.$fetch(this.$api.postUpgrade, params).then(res => {
            if (res.code === 0) {
              this.$message({
                type: 'success',
                message: `${el.recruitmentState >= 0 ? '下架' : '上架'}成功!`
              })
              this.getList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: `已取消${el.recruitmentState >= 0 ? '下架' : '上架'}`
          })         
        })
      },
      delPosition (el) { // 删除职位
        this.$confirm('此操作将永久删除该职位, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let url = `${this.$api.postDel}/${el.id}`
          this.$fetch(url).then(res => {
            if (res.code === 0) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getList()
            }
          }) 
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })         
        })
      },
      // 编辑职位
      edit (el) {
        this.$router.push({path: '/position/edit', query: {id: el}})
      },
      screenClick () {
        this.$router.push({path: '/index/3'})
      },
      init(){
        this.getKeyWords()
        this.getList()
      },
      search() {
        this.$router.push({path: this.$route.path, query: {keywords: this.listQuery.query, page: 1}})
      },
      getList() { // 请求职位数据
        this.listLoading = true
        fetchList(this.listQuery,"").then(response => {
          this.list = response.page.records
          this.listLoading = false
        }).catch(err=>{
          this.listLoading = false
        })
      },
      getKeyWords(){
        let data = this.$route.query
        if (data) {
          if (data.keywords) {
            this.listQuery.query = data.keywords
          } else {
            this.listQuery.query = ''
          }
        }
      }
    },
    watch: {
      '$route'(to, from) {
        this.init()
      }
    },
    filters: {
      recruitState (el) {
        if (el === 0) {
          return '在招'
        } else if (el === -1) {
          return '停止招聘'
        } else if (el === -2) {
          return '系统强制下架'
        } else if (el === 1) {
          return '紧急招聘'
        } else {
          return '未知'
        }
      }
    },
    
  }
</script>

